import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/home/index.vue'),
      meta: {
        title: '首页'
      }
    },
    {
      path: '/:pathMatch(.*)',
      redirect: '/404',
      meta: {
        title: '页面未找到'
      },
      children: [
        {
          path: '/404',
          name: '404',
          component: () => import('../views/error/404.vue'),
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/index.vue'),
      meta: {
        title: '登录'
      }
    },

  ]
})

//路由前置守卫
router.beforeEach(async (to, from, next) => {
  if (to.meta.requireAuth && !sessionStorage.getItem('token')) {

    ElMessage.error('请登录')
    next('/login')

  } else {
    document.title = to.meta.title + ' - ANTI FRAUD防骗后台' || 'ANTI FRAUD防骗后台'
    next()
  }
})

export default router
